/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MAPP_MODULE_
#include "mbapp.h"
#include "os_obj.h" // RTOS object definitions
#include "common.h"
#include "systask.h"

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;
uint32_t TimeCnt;

uint8_t SenSndBuff[32];

void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt);
void mbsimulink(void);

/*****************************************************************************/ /*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ModbusTask(void *argument)
{
    rt_uint32_t usData;

    /*
    mb01_Init(MB_RTU_SLAVE, 19200, MB_PAR_EVEN);
    mb.obj01.slave_id = 2;
    mb.obj01.os_event_send = mb_os_event_send;
    mb.obj01.os_rtu_end_event = 0x01;
    mb.obj01.os_timeout_event = 0x02;
    */
    
    mb02_Init(MB_RTU_SLAVE, 19200, MB_PAR_EVEN);
    mb.obj02.slave_id = 2;
    mb.obj02.os_event_send = mb_os_event_send;
    mb.obj02.os_rtu_end_event = 0x01;
    mb.obj02.os_timeout_event = 0x02;
    
    
    mb03_Init(MB_RTU_SLAVE, 19200, MB_PAR_EVEN);
    mb.obj03.slave_id = 2;
    mb.obj03.os_event_send = mb_os_event_send;
    mb.obj03.os_rtu_end_event = 0x01;
    mb.obj03.os_timeout_event = 0x02;
    
    
    //mb08_Init(MB_RTU_SLAVE, 19200, MB_PAR_EVEN);
    mb.obj08.slave_id = 2;
    mb.obj08.os_event_send = mb_os_event_send;
    mb.obj08.os_rtu_end_event = 0x01;
    mb.obj08.os_timeout_event = 0x02;
    
    for (;;)
    {
        usData = 0;
        if (RT_EOK == rt_event_recv(os_obj.eid_modbus, MBAPP_EVENT_MSK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 500, &usData))
        {
            //if(usData & MBAPP_COMX_EVENT_MASK)
        }
        else
        {
            NOP2();
        }
        
        //mb_poll(&mb.obj01);
        //mb_poll(&mb.obj02);
        mb.obj02.api->mb_poll();
        
        //mb_poll(&mb.obj03);
        mb.obj03.api->mb_poll();
        
        //mb_poll(&mb.obj08);
        
        mbsimulink();
    }
}

/*****************************************************************************/ /*!
* @brief   	modbus simulink.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void mbsimulink(void)
{
    sig_time_type sig_tm;
    
    regDevice = 0x5000;
    regVersion = 0x1000;
    regFunction = 0x83;

    RegTempPv = 120;
    RegTempSv = 120;
    RegHumiPv = 120;
    RegHumiSv = 120;
    RegColPv = 120;
    RegColSv = 120;
    RegSpeedPv = 120;
    RegSpeedSv = 120; 
    
    sig_tm = sys_run_st.sig_time;
    
    RegHolding[0x10].uVal = sig_tm.time.year;
    RegHolding[0x11].uVal = sig_tm.time.mon;
    RegHolding[0x12].uVal = sig_tm.time.day;
    RegHolding[0x13].uVal = sig_tm.time.hour;
    RegHolding[0x14].uVal = sig_tm.time.min;
    RegHolding[0x15].uVal = sig_tm.time.sec2t;
}
